﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using ISC.MapDotNetServer.Common;
using ISC.MapDotNetServer.Common.Data;
using ISC.MapDotNetServer.Common.Maps;
using OI.Framework;

namespace OI.Async
{
    public class MDNMashupSearchQuery : MDNFeatureQueryBase
    {
        public Layer Layer { get; private set; }
        public string Where { get; private set; }

        public MDNMashupSearchQuery(Layer layer, string where)
        {
            Layer = layer;
            Where = where;
        }


        protected override QueryRequest BuildQueryRequest()
        {
            return new QueryRequest
            {
                ReturnShapes = true,
                ReturnShapeBounds = true,
                MaxRows = Configuration.MaxSearchQueryRows,
                ReturnResultSetBounds = false,
                ShapeFilterOperation = ShapeFilterOperations.NO_OPERATION,
                ResultShapeCoordSys = Utility.SphericalMercatorCoordSys,
                ResultShapeBufferDistance = 0.0,
                ReturnTypes = false
            };
        }


        protected override System.Collections.Generic.IEnumerable<Query> BuildQueries()
        {
            var query = new Query
            {
                DataSourceSpecification = CreateDataSourceSpecication(Layer.ConnectionSpecification.DataTable, Utility.SphericalMercatorCoordSys, GeometryTypes.UNINITIALIZED, Where),
                DataSelectionKeyword = Query.DataSelectionKeywords.All,
            };

            // here is where we specify which colums to return
            if (Layer.HasDrilldownColumns())
            {
                query.Fields.AddRange(Layer.GetDrilldownColumns().Split(','));
            }
            else
            {
                query.Fields.Add("*");
            }

            yield return query;
        }
    }
}
